public class MySingleList {
    public ListNode head;//头引用

    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public void addFirst(int data) {
        //头插法
        ListNode newnode = new ListNode(data);
        newnode.next = head;
        head = newnode;
    }

    public void addLast(int data) {
        //尾插法
        ListNode newnode = new ListNode(data);
        ListNode tail = head;
        if (head == null) {
            head = newnode;
        } else {
            while (tail.next != null) {
                tail = tail.next;
            }
            tail.next = newnode;
        }
    }

    public void addIndex(int index, int data) {
        //任意位置插入,第一个数据节点为 0号下标
        if (index < 0 || index > this.size()) {
            throw new IndexOutOfBoundsException("index不合法");
        }
        if (index == 0) {
            this.addFirst(data);
        } else {
            ListNode newnode = new ListNode(data);
            ListNode prev = head;
            int count = 0;
            while (count + 1 != index) {
                prev = prev.next;
                count++;
            }
            newnode.next = prev.next;
            prev.next = newnode;
        }
    }

    public boolean contains(int key) {
        //查找是否包含关键字key是否在单链表当中
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void remove(int key) {
        //删除第一次出现关键字为key的节点
        if (head == null) {
            throw new NullPointerException("链表已空");
        }
        ListNode cur = head;
        if (head.val == key) {
            head = head.next;
        } else {
            while (cur.next != null) {
                if (cur.next.val != key) {
                    cur = cur.next;
                } else {
                    ListNode next = cur.next.next;
                    cur.next.next = null;
                    cur.next = next;
                }
            }
        }
    }

    public void removeAllKey(int key) {
        //删除所有值为key的节点
        if (head == null) {
            throw new NullPointerException("链表已空");
        }
        ListNode prev = head;
        ListNode cur = prev.next;
        while (cur != null) {
            if (cur.val != key) {
                cur = cur.next;
                prev = prev.next;
            } else {
                prev.next = cur.next;
                cur = prev.next;
            }
        }
        if (head.val == key) {
            head = head.next;
        }
    }

    public int size() {
        //得到单链表的长度
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void clear() {
        head = null;
    }

    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

}
